package com.can.Service.Impl;

import com.can.Service.DbTccService;
import com.can.domain.po.Game;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.can.Mapper.GameStoreMapper;
@Service
public class DbTccServiceImpl implements DbTccService {
    @Autowired
    private GameStoreMapper gameMapper;

    @Override
    public boolean prepareInsertGame(Game game, BusinessActionContext context) {
        // Try阶段：插入游戏记录（预留资源）
        gameMapper.insert(game);

        context.getActionContext().put("gameId", game.getId());
        context.getActionContext().put("gameName", game.getGamename());

        return true; // 返回Try是否成功
    }

    @Override
    public boolean dbCommit(BusinessActionContext context) {
        return true;
    }

    @Override
    public boolean dbCancel(BusinessActionContext context) {
        Integer gameId = (Integer) context.getActionContext().get("gameId");
        if (gameId != null) {
            gameMapper.deleteById(gameId); // 根据ID删除记录
        }
        return true;
    }
}